package com.greensam.pixelengine.config;

import com.greensam.pixelengine.websocket.handle.PictureEditHandler;
import com.greensam.pixelengine.websocket.handle.WsHandshakeInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket 配置类
 *
 * @author Ma Chengrui
 * @since 2025/10/31 17:56
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final PictureEditHandler pictureEditHandler;

    private final WsHandshakeInterceptor wsHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // websocket
        registry.addHandler(pictureEditHandler, "ws/picture/edit") // 添加处理器
                .addInterceptors(wsHandshakeInterceptor) //  添加拦截器
                .setAllowedOrigins("*"); // 允许跨域
    }
}
